
/*
 * 	1. adventure                 : keywords - skydiving, adventurous, outdoor
	2. nature-lover              : keywords - nature walks, nature trails, 
	3. typical-tourist             : keywords - tourist attractions, tourist
	4. artsy                          : keywords - art museums, art galleries
	5. kids                            : keywords - kids, kids activities
	6. history-geek              : keywords - history museums, historical landmarks, historical sites
	7. religious                    : keywords - religious, church
	8. romantic                    : keywords - romantic, couples activities, couples massage
 * 
 */
	

package com.android.enjoymax;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;

import com.android.enjoymax.Map.MyItemizedOverlay;
import com.google.android.maps.Overlay;
import com.yelp.v2.Business;
import com.yelp.v2.Center;
import com.yelp.v2.TimeInterval;


public class MakeItinerary {
		
	public static final int LUNCH = 1; 
	public static final int DINNER = 1;
	public static final int BREAKFAST = 1;
	public static final int BRUNCH = 1;
	
	public static final int TWO = 2;
	public static final int FOUR = 4;
	public static final int SIX = 6;
	public static final int EIGHT = 8;
	public static final int TEN = 10;
	public static final int NOON = 12; 
	public static final int FOURTEEN = 14;
	public static final int SIXTEEN = 16;
	public static final int EIGHTEEN = 18;
	public static final int TWENTY = 20;
	public static final int TWENTYTWO = 22;
	public static final int MIDNIGHT = 24;
	
	public static final int TIME_SLOT_SIZE = 4; 
	
	public static final TimeInterval SLOT_A = new TimeInterval(NOON, SIXTEEN, "F:E");  
	public static final TimeInterval SLOT_B = new TimeInterval(SIXTEEN, TWENTY, "E:F");
	public static final TimeInterval SLOT_C = new TimeInterval(TWENTY, MIDNIGHT, "F:E");
	public static final TimeInterval SLOT_D = new TimeInterval(MIDNIGHT, FOUR, "E:E");
	public static final TimeInterval SLOT_E = new TimeInterval(FOUR, EIGHT, "E:F");
	public static final TimeInterval SLOT_F = new TimeInterval(EIGHT, NOON, "F:E");
	
	public static final int MAX_RADIUS = 30; 
	
	public double latitude; 
	public double longitude;
	public Calendar calendar;
    public MyItemizedOverlay overlay;
    public List<Overlay> mapOverlay;
	
	public TimeInterval getTimeInterval(int timeNow) {
		if (SLOT_A.isValid(timeNow)) {
			return SLOT_A; 
		} 
		else if (SLOT_B.isValid(timeNow)) {
			return SLOT_B; 
		} 
		else if (SLOT_C.isValid(timeNow)) {
			return SLOT_C; 
		} 
		else if (SLOT_D.isValid(timeNow)) {
			return SLOT_D; 
		} 
		else if (SLOT_E.isValid(timeNow)) {
			return SLOT_E; 
		} 
		else if (SLOT_F.isValid(timeNow)) {
			return SLOT_F; 
		} 
		else {
			return null; 
		} 
	}
	//timeToSpend in multiples of four ONLY. 
	public ArrayList<String> listEvents(int timeToSpend) {
		ArrayList<String> eventList = new ArrayList<String>();
		int reduction = 0; 
		while (timeToSpend > 0) {
			TimeInterval tempInterval = getTimeInterval(Calendar.HOUR_OF_DAY + reduction);
			while (tempInterval.popEvent() != null) {
				eventList.add(tempInterval.popEvent()); 
			}
			timeToSpend = timeToSpend - TIME_SLOT_SIZE; 
			reduction = reduction + TIME_SLOT_SIZE; 
		}
		return eventList; 
	}

	public ArrayList<String> getKeywords(int modelNumber) {
		ArrayList<String> keywordList = new ArrayList<String>(); 
		switch (modelNumber) {
		case 1: {
			keywordList.add("skydiving"); 
			keywordList.add("adventurous"); 
			keywordList.add("outdoor"); 
			break; 
		}
		case 2: {
			keywordList.add("nature+walks"); 
			keywordList.add("nature+trails"); 
			break; 
		}
		case 3: {
			keywordList.add("tourist+attractions"); 
			keywordList.add("tourist"); 
			break; 
		}
		case 4: {
			keywordList.add("art+museums"); 
			keywordList.add("art+galleries"); 
			break; 
		}
		case 5: {
			keywordList.add("kids"); 
			keywordList.add("kids+activities"); 
			break; 
		}
		case 6: {
			keywordList.add("history+museums"); 
			keywordList.add("historical+landmarks"); 
			keywordList.add("historical+sites"); 
			break; 
		}
		case 7: {
			keywordList.add("religious"); 
			keywordList.add("church"); 
			break; 
		}
		case 8: {
			keywordList.add("romantic"); 
			keywordList.add("couples+activites"); 
			keywordList.add("couples+massage"); 
			break; 
		}
		default:
			break;
		}
		return keywordList; 
	}
	
	public ArrayList<Business> getPlacesToGo(Context c, int modelName, double longitude, double latitude, int limitItems, int moneyToSpend, int timeToSpend) {
		ArrayList<String> thingsToDo = listEvents(timeToSpend);
		ArrayList<Business> businessList = new ArrayList<Business>(); 
		
		for (String toDo : thingsToDo) {
			if (toDo.equalsIgnoreCase("e")) {
				ArrayList<String> keywords = getKeywords(modelName); 
				Center newCenter = new Center(latitude, longitude);
				int radius = MAX_RADIUS;
				for (String string : keywords) {
					GetEvents events = new GetEvents();
					events.Events(c,string, overlay, mapOverlay, newCenter, radius, limitItems);
					businessList.addAll(events.foodPlaces); 
					//Add business item to list. 
				}
			} else if (toDo.equalsIgnoreCase("f")) {
				String keywords = new String("food"); 
				Center newCenter = new Center(latitude, longitude);
				int radius = MAX_RADIUS;
				GetEvents events = new GetEvents();
				events.Events(c,keywords, overlay, mapOverlay, newCenter, radius, limitItems);
				businessList.addAll(events.foodPlaces);
			}
		}
		return businessList; 
	}
	public void setCalendar() {
		this.calendar = Calendar.getInstance(); 
	}
	public void setLatitude(double latitude) {
		this.latitude = latitude; 
	}
	public void setLongitude(double longitude) {
		this.longitude = longitude; 
	}
	public double getLongitude() {
		return longitude; 
	}
	public double getLatitude() {
		return latitude; 
	}
	public int numberMeals() {
		if (this.calendar.AM_PM == this.calendar.PM) {
			if (this.calendar.HOUR < 4) {
				return LUNCH + DINNER; 
			}
			else {
				return DINNER; 
			}
		} 
		else {
			if (this.calendar.HOUR < 9) {
				return LUNCH + DINNER + BREAKFAST + BRUNCH;
			}
			else {
				return LUNCH + DINNER + BRUNCH;
			}
		}
	}
	public int numberEvents(double availableHours) {
		if (this.calendar.AM_PM == this.calendar.PM) {
			if (this.calendar.HOUR < 4) {
				return LUNCH + DINNER; 
			}
			else {
				return DINNER; 
			}
		} 
		else {
			if (this.calendar.HOUR < 9) {
				return LUNCH + DINNER + BREAKFAST + BRUNCH;
			}
			else {
				return LUNCH + DINNER + BRUNCH;
			}
		}
	}
	
	public ArrayList<Business> makeItinerary(Context c, MyItemizedOverlay itemizedOverlay, List<Overlay> mapOverlays, int modelName, double longitude, double latitude, int moneyToSpend, int timeToSpend) {
		
		//Categories: 
		/*
		 * 1) Food - Yelp API,  
		 * 2) Fun - Google API, 
		 * 3) Hotels - Some Other API. 
		 * 
		 * (Input parameters: 
		 * a) COST (level-1, 2, 3), 
		 * b) TIME (traveling - from google directions & enjoyment - 45 to 55 minutes each), 
		 * 
		 */
		overlay = itemizedOverlay;
		mapOverlay = mapOverlays;
		
		ArrayList<Business> businessList = new ArrayList<Business>(); 
		businessList = getPlacesToGo(c,1, longitude, latitude, 5, 500, 8); 
		
//		Yelp yelp = new Yelp(); 
//		GoogleFun gFun = new GoogleFun(); 
//		Hotel hotel = new Hotel(); 
		
		Center location = new Center(); 
		location.setLatitude(latitude);
		location.setLongitude(longitude); 
		
		ArrayList<Center> returnLocations = new ArrayList<Center>(); 
		returnLocations.add(location); 
		
		return businessList; 
	}
}